<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          >添加油品</el-button
        >
        <el-button
          type="success"
          icon="el-icon-edit"
          size="mini"
          @click="handleAddAdjust"
          v-hasPermi="['station:adjust:add']"
          >调价</el-button
        >
      </el-form-item>
    </el-form>
    <el-table
      v-loading="loading"
      :data="priceList"
      ref="multipleTable"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="left" />
      <el-table-column
        label="油品价格id"
        v-if="false"
        align="left"
        prop="oilPriceId"
      />
      <el-table-column label="油品名称" align="left" prop="oilName" />
      <el-table-column label="油品价格" align="left" prop="oilPrice" />
      <el-table-column
        label="油站id"
        v-if="false"
        align="left"
        prop="stationId"
      />
      <el-table-column label="油站名称" align="left" prop="stationNanme" />
      <el-table-column label="操作时间" align="left" prop="date" />
      <el-table-column
        label="操作"
        align="left"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['station:price:remove']"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改油品价格对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" label-width="90px">
        <el-form-item label="油品名称" prop="oilName">
          <el-select
            v-model="form.oilName"
            placeholder="油品名称"
            clearable
            size="small"
            @change="onOilNameChang"
          >
            <el-option
              v-for="dict in oilNameOptions"
              :key="dict.dictLabel"
              :label="dict.dictLabel"
              :value="dict.dictLabel"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="油品价格" prop="oilPrice">
          <el-input-number
            v-model="form.oilPrice"
            :disabled="editInfo"
            :precision="2"
            :min="0"
            :step="1"
          ></el-input-number>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <!-- 添加或修改油品调价信息对话框 -->
    <el-dialog
      :title="titleAdjust"
      :visible.sync="open1"
      :rules="rules"
      width="500px"
      append-to-body
    >
      <el-form ref="form1" :model="form1" label-width="80px">
        <el-form-item label="油品名称" prop="oilName">
          <el-select
            :disabled="editInfo"
            v-model="form1.oilName"
            placeholder="油品名称"
            clearable
            size="small"
          >
            <el-option
              v-for="dict in oilNameOptions"
              :key="dict.dictLabel"
              :label="dict.dictLabel"
              :value="dict.dictLabel"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="调整价格" prop="oilAdjustPrice">
          <el-input-number
            v-model="form1.oilAdjustPrice"
            :precision="2"
            :min="0"
            :step="1"
            style="width: 150px"
          />元
        </el-form-item>
        <el-form-item label="生效状态" prop="takeEffectStatus">
          <el-radio-group v-model="form1.takeEffectStatus">
            <el-radio
              v-for="dict in takeEffectStatusOptions"
              :key="dict.dictValue"
              :label="dict.dictValue"
              >{{ dict.dictLabel }}</el-radio
            >
          </el-radio-group>
        </el-form-item>
        <el-form-item label="生效时间" prop="takeEffectDate">
          <el-date-picker
            clearable
            size="small"
            style="width: 200px"
            v-model="form1.takeEffectDate"
            type="datetime"
            value-format="yyyy-MM-dd HH:mm:ss"
            placeholder="选择生效时间"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item label="油站名称" prop="stationId">
          <el-select
            :disabled="editInfo"
            v-model="form1.stationId"
            placeholder="请选择油站"
            clearable
            size="small"
            @change="onInstitutionChang1"
          >
            <el-option
              v-for="item in stationOptions"
              :key="item.deptId"
              :label="item.deptName"
              :value="item.deptId"
            ></el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="油站名称" prop="stationName" v-show="false">
          <el-input v-model="form1.stationName" placeholder="请输入油站名称" />
        </el-form-item>
        <el-form-item label="油品价格id" v-show="false" prop="oilPriceId">
          <el-input v-model="form1.oilPriceId" placeholder="请输入油品价格id" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitFormAdjust">确 定</el-button>
        <el-button @click="cancelAdjust">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listPrice,
  delPrice,
  addPrice,
  updatePrice,
  exportPrice,
} from "@/api/station/price";
import { addAdjust, updateAdjust } from "@/api/station/adjust";
import { getDept } from "@/api/system/dept";
import { stationinfo } from "@/api/station/gun";
export default {
  name: "Price",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      editInfo: false,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 油品价格表格数据
      priceList: [],
      takeEffectStatusOptions: [],
      oilNameOptions: [],
      // 弹出层标题
      title: "",
      titleAdjust: "",
      // 是否显示弹出层
      open: false,
      open1: false,
      deptId: null,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        oilName: null,
        oilPrice: null,
        stationId: null,
        stationNanme: null,
        date: null,
      },
      query: {
        deptId: null,
      },
      stationOptions: [],
      // 表单参数
      form: {},
      form1: {},
      // 表单校验
      rules: {
        takeEffectStatus: [
          { required: true, trigger: "blur", message: "生效状态必须选择" },
        ],
        oilAdjustPrice: [
          { required: true, trigger: "blur", message: "调整价格必须慎重填写" },
        ],
      },
    };
  },
  created() {
    this.getList();
    this.getDicts("take_effect_status").then((response) => {
      this.takeEffectStatusOptions = response.data;
    });
    this.getDicts("oil_name").then((response) => {
      this.oilNameOptions = response.data;
    });
    this.query.deptId = this.$store.selectDeptId;
    if (this.query.deptId == null || this.query.deptId == "") {
      this.query.deptId = this.$store.state.user.deptId;
    }
    stationinfo(this.query).then((response) => {
      this.stationOptions = response.rows;
    });
  },
  methods: {
    /** 查询油品价格列表 */
    getList() {
      this.loading = true;
      this.queryParams.stationId = this.$store.selectDeptId;
      this.queryParams.stationId = this.$store.selectDeptId;
      if (
        this.queryParams.stationId == null ||
        this.queryParams.stationId == ""
      ) {
        this.queryParams.stationId = this.$store.state.user.deptId;
      }
      listPrice(this.queryParams).then((response) => {
        this.priceList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        oilPriceId: null,
        oilName: null,
        oilPrice: null,
        stationId: null,
        stationNanme: null,
        date: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },

    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.deptId = this.$store.selectDeptId;
      if (this.deptId == null || this.deptId == "") {
        this.deptId = this.$store.state.user.deptId;
      }
      getDept(this.deptId).then((response) => {
        this.deptInfo = response.data;
        if (this.deptInfo.jiBie == 2) {
          this.open = true;
          this.title = "添加油品价格";
          this.form.stationId = this.deptInfo.deptId;
          this.form.stationNanme = this.deptInfo.deptName;
        } else {
          this.msgSuccess("请选择油站");
        }
      });
    },
    onOilNameChang(e) {
      let obj = {};
      obj = this.oilNameOptions.find((item) => {
        //这里的userList就是上面遍历的数据源
        return item.dictLabel === e; //筛选出匹配数据
      });
      if (obj != null && obj.hasOwnProperty("dictLabel")) {
        if (obj.dictLabel === "非油品") {
          this.editInfo = true;
          this.form.oilPrice = 0;
        } else {
          this.editInfo = false;
        }
      }
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.oilPriceId != null) {
            updatePrice(this.form).then((response) => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addPrice(this.form).then((response) => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.oilPriceId);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    handleAddAdjust() {
      if (this.ids.length == 1) {
        let sss = this.$refs.multipleTable.selection;
        this.resetAdjust();
        this.form1.stationId = sss[0].stationId;
        this.form1.stationName = sss[0].stationNanme;
        this.form1.oilName = sss[0].oilName;
        this.form1.oilPriceId = sss[0].oilPriceId;
        this.form1.oilAdjustPrice = sss[0].oilPrice;
        this.form1.takeEffectStatus = "1";
        this.open1 = true;
        this.titleAdjust = "添加油品调价信息";
      } else if (this.ids.length == 0) {
        this.msgSuccess("请选择要调价的油品");
      } else {
        this.msgSuccess("只能选择一条油品进行调价");
      }
    },
    // 表单重置
    resetAdjust() {
      this.form1 = {
        adjustPriceId: null,
        oilName: null,
        oilAdjustPrice: null,
        takeEffectStatus: null,
        takeEffectDate: null,
        adjustDate: null,
        stationId: null,
        stationName: null,
        operator: null,
      };
      this.resetForm("form1");
    },
    // 取消按钮
    cancelAdjust() {
      this.open1 = false;
      this.resetAdjust();
    },
    submitFormAdjust() {
      let status = this.form1.takeEffectStatus;
      let takeEffectDate = this.form1.takeEffectDate;
      let a = true;
      if (status != null) {
        if (status == 2) {
          if (takeEffectDate == null) {
            a = false;
            this.msgSuccess("请选择生效时间");
          }
        }
      } else {
        a = false;
        this.msgSuccess("请选择生效状态");
      }
      if (a) {
        this.$refs["form1"].validate((valid) => {
          if (valid) {
            if (this.form1.adjustPriceId != null) {
              updateAdjust(this.form1).then((response) => {
                this.msgSuccess("修改成功");
                this.open1 = false;
                this.getList();
              });
            } else {
              addAdjust(this.form1).then((response) => {
                this.msgSuccess("新增成功");
                this.open1 = false;
                this.getList();
              });
            }
          }
        });
      }
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const oilPriceIds = row.oilPriceId || this.ids;

      this.$confirm("是否确认删除油品价格", "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(function () {
          return delPrice(oilPriceIds);
        })
        .then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        });
    },
    onInstitutionChang(e) {
      let obj = {};
      obj = this.stationOptions.find((item) => {
        //这里的userList就是上面遍历的数据源
        return item.deptId === e; //筛选出匹配数据
      });
      this.form.stationNanme = obj.deptName;
    },
    onInstitutionChang1(e) {
      let obj = {};
      obj = this.stationOptions.find((item) => {
        //这里的userList就是上面遍历的数据源
        return item.deptId === e; //筛选出匹配数据
      });
      this.form1.stationName = obj.deptName;
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm("是否确认导出所有油品价格数据项?", "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(function () {
          return exportPrice(queryParams);
        })
        .then((response) => {
          this.download(response.msg);
        });
    },
  },
};
</script>
